`
https://leetcode.cn/problems/spiral-matrix/
`

/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
var spiralOrder = function (matrix) {
  // 行，列
  const m = matrix.length, n = matrix[0].length

  // 边界
  let top = 0, bottom = m - 1, left = 0, right = n - 1

  // 结果
  const res = []

  while (res.length !== m * n) {
    // 从左到右
    if (top <= bottom) {
      for (let i = left; i <= right; i++) {
        res.push(matrix[top][i])
      }
      top++
    }
    // 从上到下
    if (left <= right) {
      for (let i = top; i <= bottom; i++) {
        res.push(matrix[i][right])
      }
      right--
    }
    // 从右到左
    if (top <= bottom) {
      for (let i = right; i >= left; i--) {
        res.push(matrix[bottom][i])
      }
      bottom--
    }
    // 从下到上
    if (left <= right) {
      for (let i = bottom; i >= top; i--) {
        res.push(matrix[i][left])
      }
      left++
    }
  }

  return res
};